package com.cy.ppj.service.product.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cy.ppj.comm.constant.CacheConstants;
import com.cy.ppj.dao.mapper.product.ProductTagMapper;
import com.cy.ppj.dao.mapper.product.ProductTypeMapper;
import com.cy.ppj.dao.mapper.product.ProductTypeTagMapper;
import com.cy.ppj.dao.mapper.product.TagMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.ao.product.ProductTypeTagAO;
import com.cy.ppj.model.ao.product.TagAddAO;
import com.cy.ppj.model.ao.product.TagEditAO;
import com.cy.ppj.model.domian.product.ProductTag;
import com.cy.ppj.model.domian.product.ProductTypeTag;
import com.cy.ppj.model.domian.product.Tag;
import com.cy.ppj.model.enums.TagStatus;
import com.cy.ppj.model.query.TagAdminQuery;
import com.cy.ppj.model.vo.ProductTypeTagsVO;
import com.cy.ppj.model.vo.TagDetailAdminVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.product.TagService;

/**
 * 标签service
 * @author: tanyilin
 * @create: 2018年8月4日 下午2:35:34
 */
@Service
public class TagServiceImpl extends BaseService implements TagService{

    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private ProductTagMapper productTagMapper;
    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Autowired
    private ProductTypeTagMapper productTypeTagMapper;
	
    @Override
    @Transactional
    @CacheEvict(value = CacheConstants.CACHE_PRODUCT_TAG, allEntries = true, beforeInvocation = true)
    public void addTag(TagAddAO tagAddAO) {
    	Tag tag = new Tag();
    	tag.setTag(tagAddAO.getTagName());
        int count = tagMapper.selectCount(tag);
        state(count == 0, "标签已存在");
        tag.setCreateTime(new Date());
        tag.setStatus(TagStatus.ENABLE);
        //添加-标签
        tagMapper.insertSelective(tag);
        // 添加 产品类型-标签关联
        Long[] typeIds = tagAddAO.getTypeIds();
        productTypeTagMapper.insertBatchByTypeIds(tag.getId(), typeIds);
    }

	@Override
	@Transactional
	@CacheEvict(value = CacheConstants.CACHE_PRODUCT_TAG, allEntries = true, beforeInvocation = true)
	public void updateTagStatus(Long tagId, TagStatus status) {
		Tag tag = new Tag();
		tag.setId(tagId);
		tag.setStatus(status);
		tag.setUpdateTime(new Date());
		int result = tagMapper.updateByPrimaryKeySelective(tag);
		state(result == 1, "更新标签状态异常");
	}

	@Override
	@Transactional
	@CacheEvict(value = CacheConstants.CACHE_PRODUCT_TAG, allEntries = true, beforeInvocation = true)
	public void deleteTag(Long id) {
		int result = tagMapper.deleteByPrimaryKey(id);
        state(result == 1, "删除标签失败");
        //删除产品标签关联
        ProductTag productTag = new ProductTag();
        productTag.setTagId(id);
        productTagMapper.delete(productTag);
        //删除产品类型标签关联
        ProductTypeTag productTypeTag = new ProductTypeTag();
        productTypeTag.setTagId(id);
        productTypeTagMapper.delete(productTypeTag);
	}

	@Override
	@Transactional
	@CacheEvict(value = CacheConstants.CACHE_PRODUCT_TAG, allEntries = true, beforeInvocation = true)
	public void updateTag(TagEditAO tagEditAO) {
		Tag tag = new Tag();
		tag.setId(tagEditAO.getTagId());
		tag.setTag(tagEditAO.getTagName());
		tag.setUpdateTime(new Date());
        tagMapper.updateByPrimaryKeySelective(tag);
        
        // 更新产品类型-标签
        //先根据 标签ID，删除所有它的数据
        ProductTypeTag productTypeTag = new ProductTypeTag();
        productTypeTag.setTagId(tagEditAO.getTagId());
        productTypeTagMapper.delete(productTypeTag);
        // 再重新插入新数据
        Long[] typeIds = tagEditAO.getTypeIds();
        productTypeTagMapper.insertBatchByTypeIds(tag.getId(), typeIds);
	}

	@Override
	public PageBean<Tag> adminTags(TagAdminQuery tagAdminQuery) {
		int total = tagMapper.selectAdminTagCount(tagAdminQuery);
        List<Tag> tags = new ArrayList<>();
        if (total > 0) {
        	tagAdminQuery.startPage();
            tags = tagMapper.selectAdminTags(tagAdminQuery);
        }
        return new PageBean<>(tags, total);
	}

	@Override
    @Cacheable(value = CacheConstants.CACHE_PRODUCT_TAG)
    public List<Tag> getTagsByTypeIdInCache(Long typeId) {
        return tagMapper.selectTagsByTypeId(typeId);
    }

	@Override
	@Cacheable(value = CacheConstants.CACHE_PRODUCT_TAG)
	public List<Tag> allTagsInCache() {
		//查询全部的标签，并筛选出启用状态的标签
		Tag tag = new Tag();
		tag.setStatus(TagStatus.ENABLE);
		List<Tag> tags = tagMapper.select(tag);
		tags.stream().forEach(t -> {
			t.setCreateTime(null);
			t.setUpdateTime(null);
		});
		return tags;
	}

	@Override
	@Cacheable(value = CacheConstants.CACHE_PRODUCT_TAG)
	public List<ProductTypeTagsVO> queryAllProductTypeTagsInCache() {
		return productTypeMapper.queryAllProductTypeTags();
	}

	@Override
    @Transactional
    @CacheEvict(value = CacheConstants.CACHE_PRODUCT_TAG, allEntries = true, beforeInvocation = true)
    public void addTypeTags(ProductTypeTagAO productTypeTagAO) {
        //先删除该类型原来的标签
        ProductTypeTag productTypeTag = new ProductTypeTag();
        productTypeTag.setTypeId(productTypeTagAO.getTypeId());
        productTypeTagMapper.delete(productTypeTag);
        //重新插入
        productTypeTagMapper.insertBatchWhenTagExist(productTypeTagAO.getTypeId(), productTypeTagAO.getTagIds());
    }

	@Override
	public TagDetailAdminVO selectTagDetailAdmin(Long tagId) {
		TagDetailAdminVO tagDetailAdminVO = tagMapper.selectTagDetailAdmin(tagId);
		state(tagDetailAdminVO != null, "该标签不存在");
		tagDetailAdminVO.getTypesList().stream().forEach(t -> {
			t.setCreateTime(null);
			t.setUpdateTime(null);
		});
		return tagDetailAdminVO;
	}

	
}
